package com.snopy.concurrency.demo_threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Test3
 * @description
 * @email 77912204@qq.com
 * @date 2022/02/18 15:09
 */
@Slf4j
public class Test3 {
    /*int corePoolSize,核心线程数
    int maximumPoolSize,最大线程数
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue<Runnable> workQueue*/
    private static final int COUNT_BITS = Integer.SIZE - 3;
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;//可创建最大线程数是 536870910

    // runState is stored in the high-order bits
    private static final int RUNNING    = -1 << COUNT_BITS;//-536870912
    private static final int SHUTDOWN   =  0 << COUNT_BITS;//0
    private static final int STOP       =  1 << COUNT_BITS;//536870912
    private static final int TIDYING    =  2 << COUNT_BITS;//1073741824
    private static final int TERMINATED =  3 << COUNT_BITS;//1610612736


    public static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(2,3,60,TimeUnit.SECONDS, new ArrayBlockingQueue<>(3));

    public static void main(String[] args) {
        log.info("SHUTDOWN->{},STOP->{}，TIDYING->{},TERMINATED->{},ctl->{}",SHUTDOWN,STOP,TIDYING,TERMINATED,(RUNNING|0));
        //ThreadGroup group = new ThreadGroup();
        executorService.allowCoreThreadTimeOut(true);
        log.info("线程池队列："+executorService.getQueue().size()
                +" 活着的线程数:"+executorService.getActiveCount()+
                " 线程池大小："+executorService.getPoolSize());
        for (int i = 0; i < 20; i++) {
            Thread thread = new Thread(() -> {
                log.info(Thread.currentThread().getId()  + "线程池队列：" + executorService.getQueue().size()
                        + " 活着的线程数:" + executorService.getActiveCount() +
                        " 线程池大小：" + executorService.getPoolSize());

                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"任务"+i);


            executorService.execute(thread);
        }

        if (executorService.getQueue().size() ==0){
            System.exit(0);
        }
    }
}
